<template>
  <div class="manage">
    <el-dialog
      :title="operateType === 'add' ? '新增用户' : '更新用户'"
      :visible.sync="isShow"
    >
      <common-form
        :formLabel="operateFormLabel"
        :form.sync="operateForm"
        :inline="true"
        ref="form1"
      ></common-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isShow = false">取消</el-button>
        <el-button @click="confirm" type="primary">确定</el-button>
      </div>
    </el-dialog>

    <div class="manage-header">
      <el-button type="primary" @click="addUser">+ 新增</el-button>
      <common-form
        :formLabel="searchFormLabel"
        :form.sync="searchForm"
        :inline="true"
        ref="form2"
      >
        <el-button type="primary" @click="getList(searchForm.keyword)"
          >搜索</el-button
        >
      </common-form>
    </div>

    <common-table
      :tableData="tableData"
      :tableLabel="tableLabel"
      :config.sync="config"
      @changePage="getList"
      @edit="editUser"
      @del="delUser"
    ></common-table>
  </div>
</template>

<style lang="less" scoped>
.manage {
  .manage-header {
    display: flex;
    justify-content: space-between;
    height: 40px;
  }

  .common-table {
    height: 90%;
  }
}
</style>

<script>
import CommonForm from "../../components/CommonForm.vue";
import CommonTable from "../../components/CommonTable.vue";
import { getOperateFormLabel, listUser } from "@/api/data.js";

export default {
  name: "UserView",
  components: {
    CommonForm,
    CommonTable,
  },
  data() {
    return {
      operateType: "add",
      isShow: false,
      operateFormLabel: [],
      operateForm: {
        name: "",
        addr: "",
        age: "",
        birth: "",
        sex: "",
      },
      searchFormLabel: [
        {
          model: "keyword",
          label: "",
          type: "input",
        },
      ],
      searchForm: {
        keyword: "",
      },
      tableData: [],
      tableLabel: [
        {
          prop: "name",
          label: "姓名",
        },
        {
          prop: "age",
          label: "年龄",
        },
        {
          prop: "sexLabel",
          label: "性别",
        },
        {
          prop: "birth",
          label: "出生日期",
          width: 200,
        },
        {
          prop: "addr",
          label: "地址",
          width: 320,
        },
      ],
      config: {
        page: 1,
        limit: 20,
      },
    };
  },
  methods: {
    getList(name = "") {
      this.config.loading = true;
      name ? (this.config.page = 1) : "";
      listUser({
        page: this.config.page,
        limit: this.config.limit,
        name,
      }).then((res) => {
        const result = res.data.data;
        this.tableData = result.list.map((item) => {
          item.sexLabel = item.sex === 0 ? "女" : "男";
          return item;
        });
        this.config.total = result.count;
        this.config.loading = false;
      });
    },
    confirm() {
      if (this.operateType === "edit") {
        this.$http.post("api/user/edit", this.operateForm).then(() => {
          this.isShow = false;
          this.getList();
        });
      } else {
        this.$http.post("api/user/add", this.operateForm).then(() => {
          this.isShow = false;
          this.operateForm = {
            name: "",
            addr: "",
            age: "",
            birth: "",
            sex: "",
          };
          this.getList();
        });
      }
    },
    addUser() {
      this.operateType = "add";
      this.operateForm = {
        name: "",
        addr: "",
        age: "",
        birth: "",
        sex: "",
      };
      this.isShow = true;
    },
    editUser(row) {
      this.operateType = "edit";
      this.isShow = true;
      this.operateForm = row;
    },
    delUser(row) {
      this.$confirm("此操作将永久删除该组件", "提示", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        const id = row.id;
        this.$http
          .get("api/user/delete", {
            params: {
              id,
            },
          })
          .then(() => {
            this.$message({
              type: "success",
              message: "删除成功",
            });
            this.getList();
          });
      });
    },
  },
  created() {
    this.getList();
  },
  mounted() {
    getOperateFormLabel().then((res) => {
      const { code, data } = res.data;
      if (code === 20000) {
        this.operateFormLabel = data.operateFormLabel;
      }
    });
  },
};
</script>